from common.read_xmind import ReadXmind
from generate_case import Case
import xlwt


class WriteCase:
    def __init__(self, xmind_path, sheet_name):
        ReadXmind(path=xmind_path, sheet=sheet_name).processed_data()

    @staticmethod
    def write_row(sheet, row, content):
        """
        写入第几行
        :param sheet: sheet名字
        :param row: 写入第几行
        :param content: 写入内容
        :return:
        """
        for column in range(len(content)):
            sheet.write(row, column, content[column])

    @staticmethod
    def step_handle(case):
        # 处理用例步骤
        result = []
        for step in case:
            result.append(str(case.index(step)) + "." + str(step) + '\n')
        return result

    def write(self, file_name):
        case_info = Case().case()
        excel_obj = xlwt.Workbook()
        sheet_obj = excel_obj.add_sheet('sheet1', cell_overwrite_ok=True)
        # 生成第一行
        self.write_row(sheet=sheet_obj, row=0,
                       content=['用例名称', '所属模块', '标签', '前置条件', '备注', '步骤描述', '预期结果', '用例等级', '用例状态'])
        print(case_info)
        for case in case_info:
            module_title = case["模块名称"]
            case_title = case["用例标题"]
            preconditions = case["前置条件"]
            step_case = self.step_handle(case["步骤"])
            result = self.step_handle(case["结果"])
            level = case["用例等级"]
            line = int(case_info.index(case)) + 1
            self.write_row(sheet=sheet_obj, row=line,
                           content=[case_title, module_title, '', preconditions, '', step_case, result, level,
                                    "Prepare"])
        # 判断文件夹是否存在
        # save_folder = os.getcwd().replace('\\', '/') + '/templates/ScriptFile/excel_case/'
        # if os.path.exists(save_folder) is False:
        #     os.makedirs(save_folder)
        # excel_obj.save(save_folder + file_name)
        excel_obj.save(file_name)


if __name__ == '__main__':
    WriteCase(xmind_path=r'C:\Users\admin\Desktop\NAVS.xmind', sheet_name='模板').write('test.xlsx')
